//index.js
//获取应用实例
const app = getApp()
const utils = require('../../utils/util.js')
Page({
  data: {
    userInfo: '',
    historyList: [],
    historytotalPages: 0,
    historytotal: 0,
    notstartedList: [],
    nototal: 0,
    pageNumber: 1,
    num: '',
    listFriend: [],
    conductActList: [],
    login: false,
    phone: false,
    loginShow: false,
    phoneshow: false,
  },
  login() {
    if (this.data.login) {
      this.toggle('phoneshow', true)
      this.selectComponent('#phone').showClose()
    } else {
      this.toggle('loginShow', true)
      this.selectComponent('#login').showClose()
    }
  },
  toggleToast(e) {
    console.log(e.detail)
    if (!e.detail.loginShow) {
      this.toggle('loginShow', false)
    }
    this.onShow()
  },
  toggleToast1(e) {
    console.log(e.detail)
    if (!e.detail.phoneshow) {
      this.toggle('phoneshow', false)
    }
    this.onShow()
  },
  toggle(type, val) {
    this.setData({
      [type]: val
    });
  },
  topage(e) {
    var that = this
    var url = e.currentTarget.dataset.turn
    wx.navigateTo({
      url: url
    });
  },
  //事件处理函数
  gosrlist() {
    wx.navigateTo({
      url: '/pages/birthdaylist/birthdaylist'
    });
  },
  gofavor() {
    wx.switchTab({
      url: '/pages/favor/favor'
    })
  },
  todetails(e) {
    var id = e.currentTarget.dataset.id
    var type = e.currentTarget.dataset.acttype
    if (type == 1) {
      wx.navigateTo({
        url: '/pages/ordinaryDetails/ordinaryDetails?id=' + id,
      })
    } else {
      wx.navigateTo({
        url: '/pages/hundredDetailed/hundredDetailed?id=' + id + "&type=" + type,
      })
    }
  },
  // 近期寿星
  getlistFriend() {
    var that = this
    app.req({
      url: "blessmini/blesscommon/listFriend",
      data: {
        pageNumber: 1,
        pageSize: 10,
      },
      method: "post",
      success: function (res) {
        if (res.code == 200) {
          that.setData({
            listFriend: res.data.rows
          })
          console.log('近期寿星', that.data.listFriend)
        }
      }
    })
  },
  //进行中
  getConductAct() {
    var that = this
    app.req({
      url: "blessmini/blessold/homePageConductAct",
      data: {
      },
      method: "post",
      success: function (res) {
        if (res.code == 200) {

          for (var item in res.data) {
            if (res.data[item].birthday) {
              var time = utils.formatTime3(res.data[item].birthday)
              res.data[item].birthday = time.substring(5, 7) + '月' + time.substring(8, 10) + '日'
            }
            res.data[item].startTime = utils.itemDateToShow(res.data[item].startTime, 1, true, false).substring(5)
          }
          that.setData({
            conductActList: res.data,
          })
          console.log('进行中', that.data.historyList)
        }
      }
    })
  },
  //未开始
  getNotstartedList() {
    var that = this
    app.req({
      url: "blessmini/blessold/homePageNotStarted",
      data: {
        pageNumber: 1,
        pageSize: 10,
      },
      method: "post",
      success: function (res) {
        if (res.code == 200) {
          for (var item in res.data.rows) {
            if (res.data.rows[item].birthday) {
              item.time = utils.formatTime3(res.data.rows[item].birthday)
              res.data.rows[item].birthday = item.time.substring(5, 7) + '月' + item.time.substring(8, 10) + '日'
            }
            res.data.rows[item].startTime = utils.itemDateToShow(res.data.rows[item].startTime, 1, true, false).substring(5)
          }
          that.setData({
            notstartedList: res.data.rows,
            nototal: res.data.total
          })
        }
      }
    })
  },
  // 历史活动列表
  gethistoryList() {
    var that = this
    app.req({
      url: "blessmini/blessold/homePageHistoryAct",
      data: {
        pageNumber: that.data.pageNumber,
        pageSize: 10,
      },
      method: "post",
      success: function (res) {
        if (res.code == 200) {
          for (var item in res.data.rows) {
            if (res.data.rows[item].birthday) {
              item.time = utils.formatTime3(res.data.rows[item].birthday)
              res.data.rows[item].birthday = item.time.substring(5, 7) + '月' + item.time.substring(8, 10) + '日'
            }
            res.data.rows[item].startTime = utils.itemDateToShow(res.data.rows[item].startTime, 1, true, false).substring(5)
          }
          that.setData({
            historyList: that.data.historyList.concat(res.data.rows),
            historytotalPages: res.data.totalPages,
            historytotal: res.data.total,
          })
          console.log('历史列表', that.data.historyList)
        }
      }
    })
  },
  getnoOpenCount() {
    var that = this
    app.req({
      url: 'blessmini/redPacket/noOpenCount',
      data: {},
      method: "post",
      success: function (res) {
        if (res.code == 200) {
          that.setData({
            num: res.data.count
          })
        }
      }
    })
  },
  onLoad: function () {
    var that = this
  },
  onShow: function () {
    var that = this
    that.setData({
      historyList: [],
      pageNumber: 1,
    })
    console.log('隐藏1', that.data.phoneshow)
    if (wx.getStorageSync('uid') == '') {
      that.toggle("login", false);
      console.log('隐藏2', that.data.phoneshow)
    } else {
      that.toggle("login", true);
      console.log('隐藏3', that.data.phoneshow)
      var phone = wx.getStorageSync("phone")
      if (phone == '') {
        that.toggle("phone", true);
      } else {
        that.toggle("phone", false);
        that.toggle("phoneshow", false);
        that.toggle("loginShow", false);
        console.log('隐藏4', that.data.phoneshow)
        app.getUserInfo(function (userInfo) {
          that.toggle("userInfo", userInfo);
          that.getConductAct()
          that.gethistoryList()
          that.getlistFriend()
          that.getNotstartedList()
          that.getnoOpenCount()
        })
      }
    }
  },
  onReachBottom: function () {
    var that = this
    if (that.data.historytotalPages > that.data.pageNumber) {
      this.setData({
        pageNumber: that.data.pageNumber + 1
      })
      that.gethistoryList()
    }
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onShow();
  },
})
